package com.example.myapp.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.myapp.config.ConfigProperties;
import com.example.myapp.model.User;
import com.example.myapp.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;


@RestController
public class HelloController {
    @Autowired
    RedisOperator redisOperator;
    @Autowired
    ConfigProperties configProperties;
    static List<User> result = new ArrayList<>(3);
    static {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        result.add(user);
        User user1 = new User();
        user1.setId(2);
        user1.setName("李四");
        result.add(user1);
        User user2 = new User();
        user2.setId(3);
        user2.setName("王五");
        result.add(user2);

    }
    //根据id查询接口
    @RequestMapping("/getUser/{id}")
    public User getUserById(@PathVariable Integer id) {
        System.out.println(configProperties.getFrom());
        return  result.get(id-1);
    }


    //更新接口
    @PostMapping("/updateUser")
    public User updateUserById(Integer id, String name) {
        User user = result.get(id-1);
        user.setName(name);
        System.out.println(name);
        return user;
    }

    //根据id查询接口
    @GetMapping("/getUser1")
    public Object getUserById1(@RequestBody JSONObject requestInfo) {

        return  result.get(requestInfo.getInteger("id")-1);
    }
}
